package com.example.animdemo.rigidanim;

import com.example.animdemo.base.BaseObject;
import com.example.animdemo.render.DrawableObject;
import com.example.animdemo.render.QuadBitmap;
import com.example.animdemo.render.RenderComponent;
import com.example.animdemo.render.SpriteSheet;
import com.example.animdemo.render.Texture;

public class RigidRenderComponent extends RenderComponent {
	private final int jointID;
	private final SpriteSheet sheet;
	private final RigidObject rigObject;
	
	public RigidRenderComponent(int jointID, SpriteSheet sheet, Texture texture, RigidObject rigidObj) {
		this.jointID = jointID;
		this.sheet = sheet;
		this.rigObject = rigidObj;
		setDrawable(QuadBitmap.drawableTextureWith(texture));
	}
	
	@Override
	public void setDrawable(DrawableObject drawable) {
		if(drawable instanceof QuadBitmap)
		super.setDrawable(drawable);
	}
		
	@Override
	public void update(float timeDelta, BaseObject parent) {
		// TODO Auto-generated method stub
		DrawableObject drawble = getDrawable();
		if(drawble != null && rigObject != null) {
			RigidBody body = rigObject.getBody();
			int frameIndex = body.skeleton[jointID].jointInfo.frame;
			if(frameIndex != -1) {
				sheet.getFrame((QuadBitmap)drawble, frameIndex);
				((QuadBitmap)drawble).setRotation(-body.skeleton[jointID].wRot);
				((QuadBitmap)drawble).setScale(body.skeleton[jointID].jointInfo.scaleX, body.skeleton[jointID].jointInfo.scaleY);
				setDrawOffset(body.skeleton[jointID].wPosX/2, -body.skeleton[jointID].wPosY/2);
			}
		}
		super.update(timeDelta, parent);
	}
}
